package com.example.demo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Controller.SQLController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class GetListFavController {

    @PostMapping("/getListFav")
    public JSONObject getListFav(@RequestBody JSONObject requestBody) {
        String wxid = requestBody.getString("wxid");
        JSONObject json = new JSONObject();
        JSONArray arrayPost = new JSONArray();
        SQLController sqlController=new SQLController();

        try (
             Connection connection=sqlController.ConnectToSql("jdbc:mysql://localhost:3306/jidazhidemai","root","gFL123456"+"");
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT fav FROM user WHERE wxid=?");
             PreparedStatement postStatement = connection.prepareStatement("SELECT * FROM post WHERE postid=?")) {

            preparedStatement.setString(1, wxid);
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                String fav = rs.getString("fav");
                System.out.println(fav);
                List<String> postIds =new ArrayList<>();
                if (fav != null && !fav.isEmpty()) {
                    postIds= new ArrayList<String>(Arrays.asList(fav.split(",")));
                    System.out.println(postIds);
                }
                for (String postId : postIds) {
                    JSONObject data = queryDb(postId, postStatement);
                    if (data != null) {
                        arrayPost.add(data);
                    }
                }
            }
            connection.close();
            json.put("status", "COMPLETE");
            json.put("list", arrayPost);

        } catch (SQLException e) {
            e.printStackTrace();
            json.put("status", "FAIL");
        }
        return json;
    }

    public static JSONObject queryDb(String postId, PreparedStatement preparedStatement) throws SQLException {
        JSONObject data = new JSONObject();
        preparedStatement.setString(1, postId);
        ResultSet rs = preparedStatement.executeQuery();
        if (rs.next()) {
            data.put("postid", rs.getString("postid"));
            data.put("name", rs.getString("name"));
            data.put("location", rs.getString("location"));
            data.put("like", rs.getInt("like"));
            data.put("dislike", rs.getInt("dislike"));
            data.put("oldprice", rs.getDouble("oldprice"));
            data.put("newprice", rs.getDouble("newprice"));
            String comments = rs.getString("comments");
            JSONArray arrayPlayer = new JSONArray();
            arrayPlayer = JSONArray.parseArray(comments);
            if(arrayPlayer==null){
                arrayPlayer = new JSONArray();
            }
            data.put("comments", arrayPlayer.size());
            data.put("fav", true);
        } else {
            return null;
        }
        return data;
    }
}
